<?php


/**
 * Maneging XMAP sounds added in xmapConfig and xmapFiles
 * @author Baagoe
 */
class Sound extends XMap {
    public function __construct($configFile = null, $xmlMap = null) {
        parent::__construct($configFile, $xmlMap);
        $this->objectSounds = $this->itemFile['sounds']->objectSounds->children();
        $this->soundFolder = $this->folders['sounds'];
        $this->objectSoundFolder =  $this->soundFolder. "objectSounds";
    }
    
    //Creates a new object sound
    public function loadSoundFromObject($sound = null, $id = NULL, $surfaceSound = false) {
        if($this->objectSounds && !empty($sound) && !empty($id)) {
            $this->sounds["$sound"][] = $id;
            if($surfaceSound) {
                $this->sounds["$sound"][] = "surfaceSound";
            }
        }
    }
    
    //Creates a new baggroundSound
    public function loadBgSound($sound = null) {
        if($sound == null) {
            return false;
        }
        
        $this->sounds["$sound"][] = "mapBgSound";
        return true;
    }
    
    //Returns an array of sounds
    public function printSounds($startNode = null, $startFolder = null, $preload = true) {
        $folder = "";
        if(is_string($startFolder)) {
           if($this->$startFolder) {
               $folder = $this->$startFolder;
           } else {
               $folder = $startFolder;
           } 
        }
        
        if(is_string($startNode)) {
           if($this->$startNode) {
               $startNode = $this->$startNode;
           } elseif(is_object($startNode)) {
                $startNode = $startNode;
           } else {
               return false;
           }
        }
        
        if(isset($this->sounds) && is_array($this->sounds)) {
            foreach($this->sounds as $sound => $idList) {
                $s = explode("/", $sound);
                $soundStr = $sound;
                $path = $this->getFolderPath($folder, $s);
                $sound = $this->findNode($startNode, $s);
                if(is_object($sound)) {
                    if($sound->attributes()->src) {
                    $soundFile = $path. $sound->attributes()->src;
                    
                    $ret[] = $this->createAudioTag($soundFile, $idList, $preload);
                    } else {
                        $this->error("The $soundStr audio sound dosnt exist in sound.xmap.xml!");
                        return false;
                    }
                }
            }
            return $ret;
        }
        return false;
    }
    
    //Retuns an HTML5 audio tag string
    public function createAudioTag($audioFile = NULL, $idList = array(), $preload = true) {
        if(is_string($audioFile)) {
            $classList = "";
            
            if(!empty($idList) && is_array($idList)) {
                foreach($idList as $item) {
                    $classList .= $item. " "; 
                }
            }
            
            if($preload) {
                $preload = "preload";
            } else {
                $preload = "";
            }
            
            $soundID = $this->getUniID('objectSound');
            $tag = "<audio id='$soundID' class='$classList' src='$audioFile' $preload></audio>";
            return $tag;
        }
        return false;
    }
}

?>
